package com.ivt.service.impl;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.ivt.core.ex.IvtException;
import com.ivt.dao.RoleDao;
import com.ivt.dao.SpecificDao;
import com.ivt.model.Classes;
import com.ivt.model.Role;
import com.ivt.model.view.QueryCriteria;
import com.ivt.service.RoleService;
import com.ivt.utils.enums.AuditStatus;

@Service
public class RoleServiceImpl implements RoleService {

	@Resource
	private RoleDao roleDao;
	
	@Resource
	private SpecificDao specificDao;

	@Override
	public Role create(Role role) throws IvtException {

		if (null == role) {
			throw new IvtException("班级信息不能为空");
		}
		if (StringUtils.isBlank(role.getRoleName())) {
			throw new IvtException("角色名称不能为空");
		}
		

		long  c = roleDao.existsByName(role.getRoleName());
		if (0 < c) {
			throw new IvtException("角色【" + role.getRoleName() + "】已存在");
		}

		role.setRoleId(null);

		return roleDao.saveAndFlush(role);
	}

	@Override
	public Role update(Role role) throws IvtException {
		if (null == role || null == role.getRoleId()
				|| 0 >= role.getRoleId()) {
			throw new IvtException("角色信息不能为空");
		}
		if (StringUtils.isBlank(role.getRoleName())) {
			throw new IvtException("参数错误,没有要修改的项目");
		}

		if (StringUtils.isNotBlank(role.getRoleName())
				&& 0 < roleDao.existsByNameAndId(role.getRoleName(),
						role.getRoleId())) {
			throw new IvtException("班级【" + role.getRoleName() + "】已存在");
		}

		return roleDao.saveAndFlush(role);


	}


	@Override
	public Role delete(Long roleId) throws IvtException {
		if (null == roleId || 0 >= roleId) {
			throw new IvtException("参数错误");
		}

		Role role = new Role();
		role.setRoleId(roleId);
		role.setStatus(AuditStatus.NOT);
		return roleDao.saveAndFlush(role);

	}

	@Override
	public Role getRole(Long roleId) throws IvtException {
		if (null == roleId || 0 >= roleId) {
			throw new IvtException("参数错误");
		}
		return roleDao.findOne(roleId);
	}

	@Override
	public List<Role> getRole(Set<Long> roleId) throws IvtException {
		if (CollectionUtils.isEmpty(roleId)) {
			throw new IvtException("参数错误");
		}

		Iterable<Role> coll = roleDao.findAll(roleId);
		return (null == coll) ? null : Lists.newArrayList(coll);
	}

	@Override
	public long queryCount(QueryCriteria queryCriteria) throws IvtException {
		return specificDao.queryRoleCount(queryCriteria);
	}

	@Override
	public List<Role> queryClassesByCriteria(QueryCriteria queryCriteria)
			throws IvtException {
		return specificDao.queryRole(queryCriteria);
	}

	@Override
	public int deletes(Long[] roleIds) throws IvtException {
		if (ArrayUtils.isEmpty(roleIds)) {
			throw new IvtException("参数错误");
		}

		Set<Long> cleanIds = Sets.newHashSet(roleIds);
		Iterable<Role> role = null;
		role = roleDao.findAll(cleanIds);
		int i = 0;
		if (null != role) {
			for (Role cz : role) {
				cz.setStatus(AuditStatus.NOT);
				roleDao.saveAndFlush(cz);
				i++;
			}
		}
		return i;
	}

}
